#include "Socket.hpp"
#include <cstring>
#define IP "127.0.0.1"
#define PORT 8080

int main() {
    wzh::Sock client;
    client.Socket();
    client.Connect(IP, PORT);

    while (1) 
    {
        char buffer[1024];
        std::cout << "cin >> ";
        fgets(buffer, sizeof(buffer), stdin);

        // 删除输入中的换行符
        size_t len = strlen(buffer);
        if (len > 0 && buffer[len - 1] == '\n') 
        {
            buffer[len - 1] = '\0';
        }

        // 发送消息
        write(client.FD(), buffer, strlen(buffer));

        // 读取并显示服务器的响应
        int sz = read(client.FD(), buffer, sizeof(buffer) - 1);
        if (sz < 0) 
        {
            std::cout << "read failed\n";
            break;
        } 
        else if (sz > 0) 
        {
            buffer[sz] = '\0';  // 添加字符串结尾
            std::cout << "gets: " << buffer << std::endl;
        } 
        else 
        {
            std::cout << "EOF, closed\n";
            break;
        }
    }
    return 0;
}
